package edu.gatech.oad.OTConfig;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import javax.swing.*;

import net.miginfocom.swing.MigLayout;
import java.awt.CardLayout;
import java.io.*;
/**
 * This is the TrailPanel Class
 * This class contains the that will be viewed by the user along the trail
 * @authors Akbar Dhannani, Chris Adams, and Amy Lambeth
 * @version 1.0 11/4/2011
 */

public class TrailPanel extends JPanel {
	private TrailImageDisplay panel;
	private ButtonGroup rationGroup, paceGroup;
	private ButtonListener listener;
	private JButton btnRest, btnKeepGoing, btnMap,btnInventory, btnStore,btnSave;
	private JourneyController journeyCtrl;
	private JRadioButton rdbtnFilling, rdbtnMeager, rdbtnBareBones, rdbtnSteady, rdbtnStrenuous, rdbtnGrueling;
	private Wagon wagon;
	private JLabel curMoneyLabel, curRationsLabel, curMilesTraveledLabel, curMilesToNextLabel, curLocationLabel,
	playerHealthLabel, settlerOneHealthLabel, settlerTwoHealthLabel, settlerThreeHealthLabel, settlerFourHealthLabel,
	DaysOnTrailLabel, curDaysOnTrailLabel;
	private int imgIndex;
	private boolean mapActive, storeVisited;
	private  String s;
	private float multiplier;

	private static final long serialVersionUID = 5316492254188346160L;

	/**
	 * Create the panel.
	 */
	public TrailPanel(WagonController controller){
		wagon = controller.getWagon();
		rationGroup=new ButtonGroup();
		paceGroup=new ButtonGroup();
		setLayout(new MigLayout("", "[70.00][49.00][72.00][77.00][84.00][98.00]", "[grow][][][][][][][20.00][20.00][20.00][][20.00]"));
		listener = new ButtonListener();
		journeyCtrl = new JourneyController();
	
		
		panel = new TrailImageDisplay();
		mapActive = false;
		

		
		JLabel locationLabel = new JLabel("Location:");
		add(locationLabel, "cell 0 5");
		
		curLocationLabel = new JLabel("" + journeyCtrl.getTrailNavigator().getLocation());
		add(curLocationLabel, "cell 1 5");
		
		
		
		//Ration button
		rdbtnFilling = new JRadioButton("Filling");
		rdbtnFilling.setSelected(true);
		add(rdbtnFilling, "cell 0 6");
		rdbtnFilling.addActionListener(listener);
		
		//Ration button
		rdbtnMeager = new JRadioButton("Meager");
		add(rdbtnMeager, "cell 1 6");
		rdbtnMeager.addActionListener(listener);
		
		//Ration button
		rdbtnBareBones = new JRadioButton("Bare Bones");
		add(rdbtnBareBones, "cell 2 6");
		rdbtnBareBones.addActionListener(listener);
		
		//Pace button
		rdbtnSteady = new JRadioButton("Steady");
		add(rdbtnSteady, "cell 3 6");
		rdbtnSteady.addActionListener(listener);
		
		//Pace button
		rdbtnStrenuous = new JRadioButton("Strenuous");
		add(rdbtnStrenuous, "cell 4 6");
		rdbtnStrenuous.addActionListener(listener);
		
		//Pace button
		rdbtnGrueling = new JRadioButton("Grueling");
		add(rdbtnGrueling, "cell 5 6");
		rdbtnGrueling.addActionListener(listener);
		
		//Add ration and pace buttons
		rationGroup.add(rdbtnFilling);
		rationGroup.add(rdbtnMeager);
		rationGroup.add(rdbtnBareBones);
		
		paceGroup.add(rdbtnSteady);
		paceGroup.add(rdbtnStrenuous);
		paceGroup.add(rdbtnGrueling);
		
		//Sets pace on button
		if(Wagon.getPace().equals(Pace.Steady)){
			rdbtnSteady.setSelected(true);
		}
		else if(Wagon.getPace().equals(Pace.Strenuous)){
			rdbtnStrenuous.setSelected(true);
		}
		else if(Wagon.getPace().equals(Pace.Grueling)){
			rdbtnGrueling.setSelected(true);
		}
		
		//Sets ration on button
		if(Wagon.getRation().equals("Filling")){
			rdbtnFilling.setSelected(true);
		}
		else if(Wagon.getRation().equals("Meager")){
			rdbtnMeager.setSelected(true);
		}
		else if(Wagon.getRation().equals("Bare Bones")){
			rdbtnBareBones.setSelected(true);
		}
		
		//Start date
		JLabel dateLabel = new JLabel("Start Date:");
		add(dateLabel, "cell 0 7");
		
		JLabel startDateLabel = new JLabel("May 1, 1848");
		add(startDateLabel, "cell 1 7");
		
		//Days on trail
		DaysOnTrailLabel = new JLabel("Days On Trail:");
		add(DaysOnTrailLabel, "cell 2 7");
		
		curDaysOnTrailLabel = new JLabel("0");
		add(curDaysOnTrailLabel, "cell 3 7");
		
		//Keep going button
		btnKeepGoing = new JButton("Keep Going");
		add(btnKeepGoing, "cell 4 7");
		btnKeepGoing.addActionListener(listener);
		
		//Rest button
		btnRest = new JButton("Rest");
		add(btnRest, "cell 5 7");
		btnRest.addActionListener(listener);
		
		
		//Money label
		JLabel moneyLabel = new JLabel("Money:");
		moneyLabel.setHorizontalAlignment(SwingConstants.LEFT);
		add(moneyLabel, "cell 0 8");
		
		curMoneyLabel = new JLabel("" + Wagon.getInventoryController().getWagonInventory().getMoney());
		add(curMoneyLabel, "cell 1 8");
		
		//Miles traveled label
		JLabel milesTraveledLabel = new JLabel("Miles Traveled:");
		add(milesTraveledLabel, "cell 2 8");
		
		curMilesTraveledLabel = new JLabel("0");
		add(curMilesTraveledLabel, "cell 3 8");
		
		//Party members label
		btnStore = new JButton("Store");
		btnStore.setEnabled(true);
		add(btnStore, "cell 4 8");
		
		//Inventory button
		btnInventory = new JButton("Inventory");
	//	btnInventory.setToolTipText("Wagon Inventory\r\nOxen " + Wagon.getInventoryController().getWagonInventory().get(0).getQuantity()+"" +
		//		"\r\nFood\r\nClothing\r\nAmmo\r\nAxel\r\nTongue\r\nWheel");
		btnInventory.setEnabled(false);
		s = "<html>Wagon Inventory<br> Oxen: " + Wagon.getInventoryController().getWagonInventory().get(0).getQuantity()+ " <br>" +
			" Food : " + Wagon.getInventoryController().getWagonInventory().get(1).getQuantity()+ " <br> " +
			" Clothing : " + Wagon.getInventoryController().getWagonInventory().get(2).getQuantity()+ " <br> " +
			" Ammo : " + Wagon.getInventoryController().getWagonInventory().get(3).getQuantity()+ " <br> " +
			" Wheel : " + Wagon.getInventoryController().getWagonInventory().get(4).getQuantity()+ " <br> " +
		" Axle : " +  Wagon.getInventoryController().getWagonInventory().get(5).getQuantity()+ " <br> " +
		" Tongue : " +  Wagon.getInventoryController().getWagonInventory().get(6).getQuantity()+ " <br> " ;
        
		btnInventory.setToolTipText(s);
		
		add(btnInventory, "cell 5 8");
		
		//Rations label
		JLabel rationsLabel = new JLabel("Rations:");
		add(rationsLabel, "cell 0 9");
		
		curRationsLabel = new JLabel("" + Wagon.getInventoryController().getWagonInventory().get(1).getQuantity());
		add(curRationsLabel, "cell 1 9");
		
		//Miles to next stop label
		JLabel milesToNextLabel = new JLabel("Miles to Next:");
		add(milesToNextLabel, "cell 2 9");
		
		curMilesToNextLabel = new JLabel("" + journeyCtrl.getTrailNavigator().getMilesToNext());
		add(curMilesToNextLabel, "cell 3 9");
		
		//Menu button
		btnSave = new JButton("Save");
		btnSave.setEnabled(true);
		add(btnSave, "cell 4 9");
		btnSave.addActionListener(listener);
		
		//Map button
		btnMap = new JButton("Map");
		btnMap.setEnabled(true);
		add(btnMap, "cell 5 9");
		btnMap.addActionListener(listener);
		
		//Player and settler labels
		JLabel playerLabel = new JLabel("" + Wagon.getPlayer().getName());
		add(playerLabel, "cell 1 10");
		
		JLabel settlerOneLabel = new JLabel("" + Wagon.getSettlerOne().getName());
		add(settlerOneLabel, "cell 2 10");
		
		JLabel settlerTwoLabel = new JLabel("" + Wagon.getSettlerTwo().getName());
		add(settlerTwoLabel, "cell 3 10");
		
		JLabel settlerThreeLabel = new JLabel("" + Wagon.getSettlerThree().getName());
		add(settlerThreeLabel, "cell 4 10");
		
		JLabel settlerFourLabel = new JLabel("" + Wagon.getSettlerFour().getName());
		add(settlerFourLabel, "cell 5 10");
		
		
		//Player and settler health labels
		JLabel healthLabel = new JLabel("Health:");
		add(healthLabel, "cell 0 11");
		
		playerHealthLabel = new JLabel("Healthy");
		add(playerHealthLabel, "cell 1 11");
		
		settlerOneHealthLabel = new JLabel("Healthy");
		add(settlerOneHealthLabel, "cell 2 11");
		
		settlerTwoHealthLabel = new JLabel("Healthy");
		add(settlerTwoHealthLabel, "cell 3 11");
		
		settlerThreeHealthLabel = new JLabel("Healthy");
		add(settlerThreeHealthLabel, "cell 4 11");
		
		settlerFourHealthLabel = new JLabel("Healthy");
		add(settlerFourHealthLabel, "cell 5 11");
		
		add(panel, "cell 0 0 6 5,grow");
		
		multiplier = (float)1.0;
		
		updateLabels();
	}
	
	public void updateInventoryText(){
		s = "<html>Wagon Inventory<br> Oxen: " + Wagon.getInventoryController().getWagonInventory().get(0).getQuantity()+ " <br>" +
		" Food : " + Wagon.getInventoryController().getWagonInventory().get(1).getQuantity()+ " <br> " +
		" Clothing : " + Wagon.getInventoryController().getWagonInventory().get(2).getQuantity()+ " <br> " +
		" Ammo : " + Wagon.getInventoryController().getWagonInventory().get(3).getQuantity()+ " <br> " +
		" Wheel : " + Wagon.getInventoryController().getWagonInventory().get(4).getQuantity()+ " <br> " +
		" Axle : " +  Wagon.getInventoryController().getWagonInventory().get(5).getQuantity()+ " <br> " +
		" Tongue : " +  Wagon.getInventoryController().getWagonInventory().get(6).getQuantity()+ " <br> " ;
		btnInventory.setToolTipText(s);
	}
	
	public void setPaceFocus(String pace){
		if (pace.equalsIgnoreCase("Grueling"))
			rdbtnGrueling.setFocusable(true);
		else if(pace.equalsIgnoreCase("Strenuous"))
			rdbtnStrenuous.setFocusable(true);
		else
			rdbtnSteady.setFocusable(true);
	
	}
	
	public void setRationFocus(String ration){
		if (ration.equalsIgnoreCase("Meager"))
			rdbtnMeager.setFocusable(true);
		else if(ration.equalsIgnoreCase("Filling"))
			rdbtnFilling.setFocusable(true);
		else
			rdbtnBareBones.setFocusable(true);
	
	}
	public boolean getStoreVisited(){
		return storeVisited;
	}
	
	//Updates labels after each turn
	public void updateLabels(){
		curMoneyLabel.setText(""+Wagon.getInventoryController().getWagonInventory().getMoney());
		curRationsLabel.setText(""+Wagon.getInventoryController().getWagonInventory().get(1).getQuantity());
		curMilesTraveledLabel.setText(""+journeyCtrl.getJourney().getMilesTraveled());
		curMilesToNextLabel.setText(""+journeyCtrl.getTrailNavigator().getMilesToNext());
		curLocationLabel.setText("" + journeyCtrl.getTrailNavigator().getLocation());
		curDaysOnTrailLabel.setText("" + journeyCtrl.getJourney().getElapsedTime());
		curMoneyLabel.setText("" + Wagon.getInventoryController().getWagonInventory().getMoney());
		if(journeyCtrl.getTrailNavigator().getHasStore()){
			btnStore.setEnabled(true);
		}
		else{
			btnStore.setEnabled(false);
		}
		if(Wagon.getPlayer().getHealth().equals(HealthEnum.HEALTHY)){
			playerHealthLabel.setText("Healthy");
		}
		else if(Wagon.getPlayer().getHealth().equals(HealthEnum.ILL)){
			playerHealthLabel.setText("Ill");
		}
		else if(Wagon.getPlayer().getHealth().equals(HealthEnum.DEAD)){
			playerHealthLabel.setText("Dead");
		}
		
		
		if(Wagon.getSettlerOne().getHealth().equals(HealthEnum.HEALTHY)){
			settlerOneHealthLabel.setText("Healthy");
		}
		else if(Wagon.getSettlerOne().getHealth().equals(HealthEnum.ILL)){
			settlerOneHealthLabel.setText("Ill");
		}
		else if(Wagon.getSettlerOne().getHealth().equals(HealthEnum.DEAD)){
			settlerOneHealthLabel.setText("Dead");
		}
		
		
		if(Wagon.getSettlerTwo().getHealth().equals(HealthEnum.HEALTHY)){
			settlerTwoHealthLabel.setText("Healthy");
		}
		else if(Wagon.getSettlerTwo().getHealth().equals(HealthEnum.ILL)){
			settlerTwoHealthLabel.setText("Ill");
		}
		else if(Wagon.getSettlerTwo().getHealth().equals(HealthEnum.DEAD)){
			settlerTwoHealthLabel.setText("Dead");
		}
		
		if(Wagon.getSettlerThree().getHealth().equals(HealthEnum.HEALTHY)){
			settlerThreeHealthLabel.setText("Healthy");
		}
		else if(Wagon.getSettlerThree().getHealth().equals(HealthEnum.ILL)){
			settlerThreeHealthLabel.setText("Ill");
		}
		else if(Wagon.getSettlerThree().getHealth().equals(HealthEnum.DEAD)){
			settlerThreeHealthLabel.setText("Dead");
		}
		
		if(Wagon.getSettlerFour().getHealth().equals(HealthEnum.HEALTHY)){
			settlerFourHealthLabel.setText("Healthy");
		}
		else if(Wagon.getSettlerFour().getHealth().equals(HealthEnum.ILL)){
			settlerFourHealthLabel.setText("Ill");
		}
		else if(Wagon.getSettlerFour().getHealth().equals(HealthEnum.DEAD)){
			settlerFourHealthLabel.setText("Dead");
		}
	}
	
	public JButton getBtnStore(){
			return btnStore;
	}
	
	public float getMultiplier(){
		return multiplier;
	}
	
	//public JourneyController getJourneyController(){
	//	return journeyCtrl;
	//}
	
	public JourneyController getJourneyController() {
		return journeyCtrl;
	}
	
	//Private class that listens for all buttons
	private class ButtonListener implements ActionListener{
		int randomCounter = 0;
		public void actionPerformed(ActionEvent event){
			
			updateInventoryText();
			updateLabels();
			if(event.getSource().equals(btnRest)){
				journeyCtrl.rest();
				if(Wagon.getInventoryController().getWagonInventory().get(1).getQuantity() == 0){
					JOptionPane.showMessageDialog(null, "You are out of food! The game is over!");
					System.exit(0);
				}
				updateLabels();
			}
			else if(event.getSource().equals(btnKeepGoing)){
				randomCounter ++;
				journeyCtrl.proceed();
				if(randomCounter >= 3){
					journeyCtrl.randomEvent();
					randomCounter = 0;
				}
				if(journeyCtrl.getTrailNavigator().getLocation() == "Independence"){
					multiplier = (float) 1.0;
				}
				else if(journeyCtrl.getTrailNavigator().getLocation() == "Fort Kearny"){
					multiplier = (float) 1.2;
				}
				else if(journeyCtrl.getTrailNavigator().getLocation() == "Fort Laramie"){
					multiplier = (float) 1.4;
				}
				else if(journeyCtrl.getTrailNavigator().getLocation() == "Fort Hall"){
					multiplier = (float) 1.6;
				}
				else if(journeyCtrl.getTrailNavigator().getLocation() == "Fort Boise"){
					multiplier = (float) 1.8;
				}
				else if(journeyCtrl.getTrailNavigator().getLocation() == "Fort Walla Walla"){
					multiplier = (float) 2.0;
				}
				else if(journeyCtrl.getTrailNavigator().getLocation() == "Fort Vancouver"){
					multiplier = (float) 2.2;
				}
				if(Wagon.getInventoryController().getWagonInventory().get(1).getQuantity() == 0){
					JOptionPane.showMessageDialog(null, "You are out of food! The game is over!");
					System.exit(0);
				}
				if(journeyCtrl.getTrailNavigator().getIsRiver()){
					// Make sure to set image to river image
					panel.changeDisplayImage(32); // Set image to river crossing
					Object [] riverOptions = {"Caulk", "Ford","Ferry"};
					JOptionPane pane = new JOptionPane("You have arrived at " + journeyCtrl.getTrailNavigator().getLocation()+ " please choose one of the options below", JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_OPTION, null, riverOptions, riverOptions[0]);
					JDialog dialog = pane.createDialog(null, "Title");
					dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
					dialog.setVisible(true);
					String answer = (String) pane.getValue();
					if (answer.equals("Caulk")) {
						journeyCtrl.riverCrossing("Caulk");
					} 
					else if (answer.equals("Ford")) {
						journeyCtrl.riverCrossing("Ford");
					}
					else {
						Wagon.getInventoryController().getWagonInventory().setMoney(Wagon.getInventoryController().getWagonInventory().getMoney()-25);
						JOptionPane.showMessageDialog(null, "You have successfully crossed the river! The ferry has charged a fee of $25.");
					}
					
					dialog.dispose();

					
				}
				updateLabels();
				updateInventoryText();
				panel.changeDisplayImage(31); // Set image back to trail
				storeVisited = false;
			}
			else if(event.getSource().equals(rdbtnFilling)){
				Wagon.setRation("Filling");
			}
			else if(event.getSource().equals(rdbtnMeager)){
				Wagon.setRation("Meager");
			}
			else if(event.getSource().equals(rdbtnBareBones)){
				Wagon.setRation("Bare Bones");
			}
			else if(event.getSource().equals(rdbtnSteady)){
				Wagon.setPace(Pace.Steady);
			}
			else if(event.getSource().equals(rdbtnStrenuous)){
				Wagon.setPace(Pace.Strenuous);
			}
			else if(event.getSource().equals(rdbtnGrueling)){
				Wagon.setPace(Pace.Grueling);
			}
			if(event.getSource().equals(btnStore)){
				storeVisited = true;
			}
			if(event.getSource().equals(btnMap)){
				if(!mapActive){
					imgIndex = journeyCtrl.getTrailNavigator().getCurrentIndex();
					panel.changeDisplayImage(imgIndex);
					panel.repaint();
					panel.revalidate();
					revalidate();
					repaint();
					mapActive = true;
					btnKeepGoing.setEnabled(false);
					btnRest.setEnabled(false);
					

				}
				else{
					panel.changeDisplayImage(31);
					panel.repaint();
					panel.revalidate();
					revalidate();
					repaint();
					mapActive = false;
					btnKeepGoing.setEnabled(true);
					btnRest.setEnabled(true);
				}
			}
			if(event.getSource().equals(btnSave)){
				File file = new File("data.txt");
				try {
					BufferedWriter writer = new BufferedWriter(new FileWriter(file));
					writer.write(Wagon.getSettlerOne().getName());
					writer.newLine();
					writer.write("" + Wagon.getSettlerOne().getHealth());
					writer.newLine();
					writer.write((Wagon.getSettlerTwo().getName()));
					writer.newLine();
					writer.write( "" + Wagon.getSettlerTwo().getHealth());
					writer.newLine();
					writer.write((Wagon.getSettlerThree().getName()));
					writer.newLine();
					writer.write(("" + Wagon.getSettlerThree().getHealth()));
					writer.newLine();
					writer.write((Wagon.getSettlerFour().getName()));
					writer.newLine();
					writer.write(("" + Wagon.getSettlerFour().getHealth()));
					writer.newLine();
					writer.write((Wagon.getPlayer().getName()));
					writer.newLine();
					writer.write(("" + Wagon.getPlayer().getHealth()));
					writer.newLine();
					writer.write(("" + Wagon.getPlayer().getProfession()));
					writer.newLine();
					writer.write(("" + Wagon.getPace()));
					writer.newLine();
					writer.write(("" + Wagon.getRation()));
					writer.newLine();
					
					writer.write(("" + Wagon.getInventoryController().getWagonInventory().get(0).getQuantity()));
					writer.newLine();
					writer.write(("" + Wagon.getInventoryController().getWagonInventory().get(1).getQuantity()));
					writer.newLine();
					writer.write(("" + Wagon.getInventoryController().getWagonInventory().get(2).getQuantity()));
					writer.newLine();
					writer.write(("" +Wagon.getInventoryController().getWagonInventory().get(3).getQuantity()));
					writer.newLine();
					writer.write(("" + Wagon.getInventoryController().getWagonInventory().get(4).getQuantity()));
					writer.newLine();
					writer.write(("" + Wagon.getInventoryController().getWagonInventory().get(5).getQuantity()));
					writer.newLine();
					writer.write(("" + Wagon.getInventoryController().getWagonInventory().get(6).getQuantity()));
					writer.newLine();
					writer.write((""+journeyCtrl.getTrailNavigator().getLocation()));
					writer.newLine();
					writer.write((""+journeyCtrl.getJourney().getElapsedTime()));
					writer.newLine();
					writer.write((""+journeyCtrl.getJourney().getMilesTraveled()));
					writer.newLine();
					writer.write(("" + journeyCtrl.getTrailNavigator().getMilesToNext()));
					writer.newLine();
					writer.write(("" + journeyCtrl.getTrailNavigator().getCurrentIndex()));
					writer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
	}
}


